import rpc.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 15:43 2021/1/28
 * @Modified By:
 */
public class RemoteInvocationHandler implements InvocationHandler {
    private String host;

    private int port;

    public RemoteInvocationHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 请求数据包装
        RpcRequest request = new RpcRequest();
        request.setClassName(method.getDeclaringClass().getName());
        request.setMethodName(method.getName());
        request.setParameters(args);
        // 远程通信
        RpcNetTransport transport = new RpcNetTransport(host,port);
        Object result = transport.send(request);
        return result;
    }
}
